package com.java.service;

import java.util.Date;
import java.util.List;

import org.apache.commons.lang.StringUtils;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import com.java.common.CommonUtil;
import com.java.entity.QIndex;
import com.java.entity.QList;
import com.java.entity.SupplierQuotation;

/**
 * 产品管理服务
 * @author wqf
 * @version  Nov 24, 2011 11:51:06 AM
 */
@Transactional(propagation = Propagation.REQUIRED,readOnly=false, rollbackFor = Exception.class)
public class QuotationService extends BaseService{

	/**
	 * 添加产品
	 * @param role
	 */
	public void addQIndex(QIndex qindex){
		this.dao.saveObject(qindex);
	}
	
	/**
	 * 添加产品
	 * @param role
	 */
	public void addQList(QList qindex){
		this.dao.saveObject(qindex);
	}
	
	/**
	 * 编辑产品（for dwr）
	 * @param product
	 */
	public void editQIndex(QIndex qindex){
		if(null == qindex.getId()){
			this.addQIndex(qindex);
		}else{
			this.updateQIndex(qindex);
		}
	}	
	
	/**
	 * 编辑产品（for dwr）
	 * @param product
	 */
	public void editQList(QList qindex){
		if(null == qindex.getId()){
			this.addQList(qindex);
		}else{
			this.updateQList(qindex);
		}
	}	
	
	/**
	 * 修改产品
	 * @param product
	 */
	public void updateQIndex(QIndex qindex){
		this.dao.updateObject(qindex);
	}
	
	/**
	 * 修改产品
	 * @param product
	 */
	public void updateQList(QList qindex){
		this.dao.updateObject(qindex);
	}
	
	/**
	 * 删除产品
	 * @param id
	 */
	public void deleteQIndex(Long id){
		QIndex qindex = this.dao.loadObject(QIndex.class, id);
		this.dao.deleteObject(qindex);
	}
	
	/**
	 * 删除产品
	 * @param id
	 */
	public void deleteQList(Long id){
		QList qindex = this.dao.loadObject(QList.class, id);
		this.dao.deleteObject(qindex);
	}

	public void deleteQIndexByIds(String ids){
		if(StringUtils.isNotBlank(ids)){
			String []id  = ids.split(",");
			for(String s:id){
				deleteQIndex(Long.parseLong(s));
			}			
		}	
	}
	
	public void deleteQListByIds(String ids){
		if(StringUtils.isNotBlank(ids)){
			String []id  = ids.split(",");
			for(String s:id){
				deleteQList(Long.parseLong(s));
			}			
		}	
	}
	
	public QIndex getQIndexById(Long id){
		return this.dao.loadObject(QIndex.class, id);
	}
	
	/**
	 * 报价单号重复
	 * @param id
	 * @return
	 */
	public boolean existsQIndexByQId(Long id,String qid){
		QIndex data = this.dao.loadObject(QIndex.class, "id!=? and qid=?", id,qid);
		if(data!=null) return true;
		return false;
	}
	
	public QList getQListById(Long id){
		return this.dao.loadObject(QList.class, id);
	}
		
	public List<QList> getQListByQIndexId(Long id){
		return this.dao.loadObjects(QList.class, "qid.id=?", null, null, null, id);
	}
	
	public String getQIndexMaxPid(String qid){
		//List<String> data = this.dao.loadObjectsNativeSQL("select case when max(qid) is null then '"+qid+"001' else max(qid) end as id from qindex where qid like '"+qid+"%'");
		List<QIndex> data = this.dao.loadObjectsNativeSQL(QIndex.class,"select * from qindex where qid=(select max(qid) from qindex where qid like '"+qid+"%')");
		String q = "";
		if(data!=null&&data.size()>0){
			q = (data.get(0).getQid()==null)?"001":data.get(0).getQid().substring(qid.length());
			q = CommonUtil.haoAddOne(q, 3);
		}else{
			q = "001";
		}
		return qid+q;
	}
	
	public List<QIndex> getQIndexByIds(String ids){
		if(StringUtils.isNotBlank(ids)){
			return this.dao.loadObjects(QIndex.class, "id in(" + ids + ")", "id desc", null, null);			
		}
		return null;
	}
	
	
}
